<?php 
class Order_model extends CI_Model
{
    public $table = 'store_order';
    function __construct()
    {
        parent::__construct();
    }

    function getAll($offset,$number)
    {
        return $this->db->order_by('order_id','desc')
            ->get($this->table,$number,$offset)->result();
    }
    function save(array $data)
    {
        $data['order_created_time']=time();     //date('Y-m-d H:i:s')
        $this->db->insert($this->table,$data);
    }
    function countRows()
    {
        return $this->db->count_all($this->table);
    }
    function update_item_status( $order_id, $status ) {
        $data = array (
            'status'	=> (int)$status
        );

        $this->db->where('order_id', (int)$order_id );
        $this->db->update( $this->table , $data );
    }
    public function searchAjax(&$total,$keyword,$start,$numberget){
        $where="";
        $keyword=explode(" ",$keyword);//CAT CHUOI RA THANH NHIEU CHUOI NHO NGAN CACH BOI DAU CACH
        foreach($keyword as $index => $keyword)//LAY TUNG PHAN TU CUA CHUOI
        {
            $where.="order_name like '%{$keyword}%' AND ";// . dung de noi cac chuoi
            //echo "lan lap thu $index : where = $where<br />";
        }
        $where=trim($where);
        if(strlen($where) > 4)//TONG SO KI TU CUA CHUOI
        {
            $where=substr($where, 0, -4);//cat 4 ki tu cuoi cung
        }
        $data= $this->db
            ->order_by("order_id",'desc')
            ->where($where)
            ->get($this->table,$numberget,$start)
            ->result();

        $rs = $this->db->where($where)->count_all_results($this->table);
        $total = $rs;
        return $data;
    }
    function getRules()//ham dat quy tac cho du lieu
    {
        return array(
            array(
                'field'=>'order_name',//ten truong trong data
                'label'=>'order name',//nhan vo van
                'rules'=>'trim|required'//bat buoc
            ),  
            array(
                'field'=>'order_email',//ten truong trong data
                'label'=>'order email',//nhan vo van
                'rules'=>'trim|required|valid_email'//
            ),  
            array(
                'field'=>'order_phone_no',//ten truong trong data
                'label'=>'order phone no',//nhan vo van
                'rules'=>'required|is_natural'//
            ),
            array(
                'field'=>'order_deliver_method',//ten truong trong data
                'label'=>'order delever method',//nhan vo van
                'rules'=>'required'//
           ),
           array(
                'field'=>'order_address',//ten truong trong data
                'label'=>'order address',//nhan vo van
                'rules'=>'required'//
           ),  
           array(
                'field'=>'order_comment',//ten truong trong data
                'label'=>'order comment',//nhan vo van
                'rules'=>'required'//
           ),       
        );
    }
}